package top.zhangjianyong.tools.remind;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import top.zhangjianyong.tools.remind.strategy.RemindStrategy;
import top.zhangjianyong.tools.utils.Mail;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ： zhangjianyong
 * @date ： 2022/10/9 4:09 PM
 */
@Slf4j
@Component
public class RemindStrategyHolder implements ApplicationRunner {

    public final static Map<String, RemindStrategy> MAP = new HashMap<>();

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("[提醒策略] 加载策略");
        Map<String, RemindStrategy> beansOfType = applicationContext.getBeansOfType(RemindStrategy.class);
        beansOfType.forEach((k, v) -> {
            MAP.put(v.type(), v);
            log.info("[提醒策略] 加载 {}", v.type());
        });
        log.info("[提醒策略] 加载结束");
    }

    public RemindStrategy getStrategy(String type) {
        if (MAP.containsKey(type)) {
            return MAP.get(type);
        }
        return null;
    }

    public List<String> resetType() {
        return MAP.values().stream()
                .filter(RemindStrategy::reset)
                .map(RemindStrategy::type)
                .collect(Collectors.toList());

    }
}
